<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>宇成建筑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" href="../statics/css/font.css">
    <link rel="stylesheet" href="../statics/plugins/layui/css/layui.css">
    <link rel="stylesheet" href="../statics/css/xadmin.css">
    <link rel="stylesheet" href="../statics/css/style.css">
</head>

<body class='labelAuto'>
    <div class="x-nav">
        <span class="layui-breadcrumb">
            <a href="welcome.html">首页</a>
            <a href="javascript:;">供应商管理</a>
            <a>
                <cite>供应商列表</cite>
            </a>
        </span>
        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
            onclick="location.reload()" title="刷新">
            <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
        </a>
        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right;margin-right:10px"
            onclick="window.history.go(-1)" title="后退">
            <i class="iconfontBlod" style="line-height:30px">&#xe74f;</i>
        </a>
    </div>
    <div class="layui-fluid">
        <div class="layui-d layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-body search">
                        <form action="" class="layui-form livingList">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">时间</label>
                                    <div class="layui-input-block">
                                        <select name="" id="year">
                                            <option value="null">-</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn search" type="button" id='search'>
                                        <i class="iconfont layui-icon">&#xe615;</i>
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="layui-card-body tableCardBody">
                        <table class="layui-table" id="lists" lay-filter="test"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
<script src="../statics/plugins/layui/layui.js" charset="utf-8"></script>
<script src='../statics/plugins/jQuery-3.2.1/jquery-3.2.1.min.js'></script>
<script type="text/javascript" src="../statics/plugins/X-admin/xadmin.js"></script>
<script type="text/javascript" src="../statics/js/public.js?v=1129"></script>
<script type="text/javascript">
    layui.use(['form', 'layer', "table", "laydate"], function () {
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var layer = layui.layer;
        var supplierId = Number(localStorage.getItem('supplierId'));
        var date=new Date();
        var year=date.getFullYear();
        var option='';
        for(var i=0;i<12;i++){
            if(i<9){
                option+=`
                    <option>${year+'-'+('0'+(i+1))}</option>
                `
            }else{
                option+=`
                    <option>${year+'-'+(i+1)}</option>
                `
            }
        }
        $('#year').html('<option value="null">-</option>'+option);
        form.render();
        // 异步数据加载
        var lists = table.render({
            elem: '#lists', //绑定DOM元素
            id: 'lists', //table的id
            title: '人员列表',
            height: 'full-150',
            contentType: "application/json; charset=utf-8",
            url: url + '/project/supplier/repaymentLists', //数据接口地址
            request: {
                pageName: 'page', //页码的参数名称，默认：page
                limitName: 'length' //每页数据量的参数名，默认：limit
            },
            parseData: function(res){
				return {
					"code": res.code, //解析接口状态
                    "data": res.data.data,
                    "msg": res.msg,
                    "count":res.data.recordsTotal
				};
            },
            method: 'POST',
            where: {
                start:0,
                draw:1,
                supplierId: supplierId,
                month: null,
                token:getToken()
            },
            toolbar: '#toolbar',
            defaultToolbar: ['filter'],
            page: true, //开启分页
            limit: 10, //默认每页记录条数
            limits: [10, 20, 50], //每页记录条数可选数量
            cols: [
                [ //表头
                    { field: '', title: '编号', type: 'numbers',width:'8%'},
                    { field: 'ordersn', title: '还款金额', },
                    { field: 'totalPrice', title: '备注'},
                    { field: 'deliveryTime', title: '还款时间',templet(d){
                        if(d.deliveryTime==null){
                            return "";
                        }else{
                            return `${d.deliveryTime.substring(d.deliveryTime,(d.deliveryTime.length)-9)}`
                        }
                    }},
                    { field: 'createTime', title: '记录时间',templet(d){
                        return `${(d.createTime.toString()).substring((d.createTime.toString()),(d.createTime.toString()).length-9)}`
                    }}
                ]
            ],
            response: {
                statusName: 'code', //数据状态的字段名称，默认：code
                statusCode: 100000, //成功的状态码，默认：0
                msgName: 'msg', //状态信息的字段名称，默认：msg
                countName: 'count', //数据总数的字段名称，默认：count
                dataName: 'data' //数据列表的字段名称，默认：data
            },
            done:function(){
                permission();
            }
        });
        $('body').on('click','table thead input',function(){
            if($(this).prop('checked')) {
                $('table tbody input[type="checkbox"]').prop('checked', true);
            } else {
                $('table tbody input[type="checkbox"]').prop('checked', false);
            }
        })
        table.on('tool(test)',function(data){
            if(data.event=='details'){
                var tr = '';
                $.ajax({
                    url: url + "/project/supplier/orderInfo",
                    type: "POST",
                    async: false,
                    data: {
                        orderId: data.data.id,
                        projectId: Number(localStorage.getItem('projectRecords1_id')),
                        token:getToken()
                    },
                    success: function(r) {
                        if(r.code == 100000) {
                            if(r.data==''||r.data==null){
                                tr='';
                            }else{
                                for(var i=0;i<r.data.length;i++){
                                    tr += `
                                        <tr>
                                            <td>${i+1}</td>
                                            <td>${r.data[i].materialName}</td>
                                            <td>${r.data[i].spec}</td>
                                            <td>${r.data[i].price}</td>
                                            <td>${r.data[i].amount}</td>
                                            <td>${r.data[i].totalPrice}</td>
                                        </tr>
                                    `
                                }
                            }
                        } else {
                            tips('供应商详情',r);
                        }
                    }
                })
                layer.open({
                    type: 1,
                    title: "货单记录详情",
                    area: ['50%', 'auto'],
                    content: `
                        <div class="layui-d layui_open">
                            <div class="layui-col-md12" style='height:auto'>
                                <table class='layui-table'>
                                    <thead>
                                        <tr>
                                            <th>编号</th>
                                            <th>材料名称</th>
                                            <th>品牌规格</th>
                                            <th>售出价格</th>
                                            <th>数量</th>
                                            <th>总价</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        ${tr}
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    `,
                    btn: ['关闭'],
                    skin: 'my-skin'
                })
            }else if(data.event=="payBackMoney_btn"){//还款
                var orderIds = [data.data.id];
                layer.open({
                    type: 1,
                    title: "还款",
                    area: ['40%', 'auto'],
                    content: `
                        <form class='layui-form layerOpen'>
                            <div class="layui-form-item">
                                <label class="layui-form-label">所选货单共计${data.data.totalPrice}元</label>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">还款金额</label>
                                <div class="layui-input-block">
                                    <input type="number" min=0 class="layui-input" id="account">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">还款时间</label>
                                <div class="layui-input-block">
                                    <input type="text" readonly class="layui-input" id="repayTime">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">备注</label>
                                <div class="layui-input-block">
                                    <textarea class='layui-textarea' id='remark'></textarea>
                                </div>
                            </div>
                        </form>
                    `,
                    btn: ['确认', '取消'],
                    skin: 'my-skin',
                    success:function(){
                        laydate.render({
                            elem: '#repayTime' //指定元素
                        });
                        form.render();
                    },
                    yes: function(index, layero) {
                        var account = $('#account').val();
                        var repayTime = $('#repayTime').val();
                        var remark = $('#remark').val();
                        $.ajax({
                            url: url + "/project/supplier/batchRepay",
                            type: "POST",
                            async: false,
                            traditional: true,
                            contentType: "application/json; charset=utf-8",
                            data: JSON.stringify({
                                supplierId: data.data.id,
                                account: Number(account),
                                repayTime: repayTime,
                                remark: remark,
                                orderIds: orderIds,
                                token:getToken()
                            }),
                            success: function(r) {
                                if(r.code == 100000) {
                                    tips('还款',r);
                                    setTimeout(function(){
                                        layer.closeAll();
                                        table.reload('lists');
                                    },2000)
                                } else {
                                    tips('还款',r);
                                }
                            }
                        })
                    }
                })
            }
        })

        $('body').on('click','#search',function(){
            var search=$(this).parent().prev().find('input').val();
            table.reload('lists',{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where:{
                    "isPay":$('#isPay option:selected').val()=='null'?null:Number($('#isPay option:selected').val()),
                    "search":search
                }
            })
        })

    });
</script>

</html>